﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Manage_Emp_examination : AdminPageBase
{
    protected int planfinishid = 0;
    int StudyAnswerId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckAdmin();
        GetUserPermisions();
        GetPlanFinishId();
        GetStudyAnswerId();
        if (!IsPostBack)
        {
            GetPlanFinishClass();
            BindQuestions();
        }

    }
    protected void CheckAdmin()
    {
        if (AdminSessionHelper.UserDetails == null)
        {
            JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
        }


    }
    protected void GetPlanFinishId()
    {
        if (Request.QueryString["id"] != null)
        {
            planfinishid = Convert.ToInt32(Request.QueryString["id"]);

        }
        else
            Response.Redirect("Study.aspx");
    }
    protected void GetStudyAnswerId()
    {
        if (Request.QueryString["StudyAnswerId"] != null)
        {
            StudyAnswerId = Convert.ToInt32(Request.QueryString["StudyAnswerId"]);

        }
        else
        {
            Response.Redirect("learn.aspx?id=" + planfinishid);
        }
    }
    protected void GetPlanFinishClass()
    {

        if (planfinishid != 0)
        {
            if (AdminSessionHelper.UserDetails != null)
            {
                DataSet ds = jtDataAccess.GetStudyClass(planfinishid, Convert.ToInt32(AdminSessionHelper.UserDetails["UserId"].ToString()));
                if (CoreTools.CheckDataSet(ds))
                {
                    lblClassName.Text = ds.Tables[0].Rows[0]["Name"].ToString();

                }
            }
            else
                JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
        }
        else
        {
            Response.Redirect("Study.aspx");
        }
    }
    //protected void lbtnStudy_Click(object sender, EventArgs e)
    //{
    //    Response.Redirect("learn.aspx?id="+planfinishid);
    //}
    protected void repQuestion_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            HiddenField hdfExercisesTypeId = (HiddenField)e.Item.FindControl("hdfExercisesTypeId");
            RadioButtonList rbtnanswer = (RadioButtonList)e.Item.FindControl("rbtnanswer");
            HiddenField hdfChoseA = (HiddenField)e.Item.FindControl("hdfChoseA");
            HiddenField hdfChoseB = (HiddenField)e.Item.FindControl("hdfChoseB");
            HiddenField hdfChoseC = (HiddenField)e.Item.FindControl("hdfChoseC");
            HiddenField hdfChoseD = (HiddenField)e.Item.FindControl("hdfChoseD");
            if (rbtnanswer != null)
            {
                rbtnanswer.Items.Clear();
                if (hdfExercisesTypeId != null && hdfExercisesTypeId.Value.Equals("2"))
                {
                    ListItem item = new ListItem("对", "对");

                    rbtnanswer.Items.Insert(0, item);
                    ListItem item1 = new ListItem("错", "错");
                    rbtnanswer.Items.Add(item1);
                }
                else if (hdfExercisesTypeId != null && hdfExercisesTypeId.Value.Equals("1"))
                {

                    if (hdfChoseA != null)
                    {
                        ListItem itema = new ListItem(hdfChoseA.Value, "A");
                        rbtnanswer.Items.Insert(0, itema);
                    }
                    if (hdfChoseB != null)
                    {
                        ListItem itemB = new ListItem(hdfChoseB.Value, "B");
                        rbtnanswer.Items.Add(itemB);
                    }
                    if (hdfChoseC != null)
                    {
                        ListItem itemC = new ListItem(hdfChoseC.Value, "C");
                        rbtnanswer.Items.Add(itemC);
                    }
                    if (hdfChoseD != null)
                    {
                        ListItem itemD = new ListItem(hdfChoseD.Value, "D");
                        rbtnanswer.Items.Add(itemD);
                    }


                }
            }
        }
    }
    protected void BindQuestions()
    {
        if (planfinishid != 0)
        {
            if (AdminSessionHelper.UserDetails != null)
            {
                DataSet ds = jtDataAccess.GetExercisesByPlanFinishId(planfinishid, Convert.ToInt32(AdminSessionHelper.UserDetails["UserId"].ToString()));
                if (CoreTools.CheckDataSet(ds))
                {
                    hdfEmpId.Value = ds.Tables[0].Rows[0]["EmpId"].ToString();
                }
                if (ds != null && ds.Tables.Count > 1 && ds.Tables[1].Rows.Count > 0)
                {
                    repQuestion.DataSource = ds.Tables[1];
                    repQuestion.DataBind();
                }
            }
            else
                JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
        }
    }
    protected void rbtnanswer_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void btnSumbit_Click(object sender, EventArgs e)
    {
        int complete = 1;
        int rightnum=0;
        int wrongnum=0;
        UserType ut = new UserType();//用户自定义类型
        DataTable AnswerTable = ut.GetAnswerTableType();
        for (int i = 0; i < repQuestion.Items.Count; i++)
        {
            RadioButtonList rbtnanswer = (RadioButtonList)repQuestion.Items[i].FindControl("rbtnanswer");
            HiddenField hdfAnswer = (HiddenField)repQuestion.Items[i].FindControl("hdfAnswer");
            HiddenField hdfExercisesId = (HiddenField)repQuestion.Items[i].FindControl("hdfExercisesId");
            if (hdfExercisesId != null && hdfExercisesId.Value != "" && rbtnanswer != null)
            {
                if ((rbtnanswer.SelectedItem==null)||string.IsNullOrEmpty(rbtnanswer.SelectedItem.Value))
                {
                    complete = 0;
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("还有题未做！"), false);
                    break;
                }
                else
                {
                    if (hdfAnswer != null && rbtnanswer.SelectedItem.Value.Equals(hdfAnswer.Value))
                        rightnum++;
                    else
                        wrongnum++;
                    DataRow AnswerTableRow;
                    AnswerTableRow = AnswerTable.NewRow();
                    AnswerTableRow["EmpId"] = Convert.ToInt32(hdfEmpId.Value);
                    AnswerTableRow["ExercisesId"] = Convert.ToInt32(hdfExercisesId.Value);
                    AnswerTableRow["Answer"] = rbtnanswer.SelectedItem.Value;
                    AnswerTable.Rows.Add(AnswerTableRow);

                }
            }

        }
        AnswerTable.AcceptChanges();
        if (complete == 1)
        {
            if (AdminSessionHelper.UserDetails != null)
            {
                DataSet ds = jtDataAccess.SaveAnswer(StudyAnswerId, AnswerTable, Convert.ToInt32(AdminSessionHelper.UserDetails["UserId"].ToString()));
                if (CoreTools.CheckDataSet(ds))
                {
                    //ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("提交成功！"), false);
                    btnSumbit.Visible = false;
                    lblPassScore.Text = ds.Tables[0].Rows[0]["PassScore"].ToString();
                    lblScore.Text = ds.Tables[0].Rows[0]["Score"].ToString();
                    lblrightnum.Text = rightnum.ToString();
                    lblwrongnum.Text = wrongnum.ToString();
                    divtongji.Visible = true;
                    floatBoxBg1.Visible = true;
                }
                else
                {
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("提交失败！"), false);
                }
            }
            else
                JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
        }

    }

    protected void ibtnclosedivtongji_Click(object sender, ImageClickEventArgs e)
    {
        divtongji.Visible = false;
        floatBoxBg1.Visible = false;
        ScriptManager.RegisterStartupScript(up1, this.GetType(), "write", "<script>self.close();</script>", false);
    }
    protected void btnok_Click(object sender, EventArgs e)
    {
        divtongji.Visible = false;
        floatBoxBg1.Visible = false;
        ScriptManager.RegisterStartupScript(up1, this.GetType(), "write","<script>self.close();</script>", false);
        //Response.Write("<script>self.close();</script>");
    }
}